#!/bin/sh
#
# 简单写一下脚本的功能。
#
#
#

if [ $# -lt 1 ]; then
	echo "Usage: $0 words"
	exit
fi

localdir="/Users/hbaolong/workshop/gitee/3place/"

dictfile="ENMYDICT/myendict.md"
dict_ee="ENMYDICT/myeedict.md"

dict_except="ENMYDICT/exceptwords.md"

badword=""

okword=""

for newword in "$@"
do
	# 字是否已经出现。

	echo $okword | grep -w -i $newword >/dev/null

	if [ $? -eq 0 ]; then

		continue
	fi

#	echo [$newword]

	okword=$okword" "$newword

	iexist=0

	grep -i "^#### "$newword" " $localdir$dictfile >/dev/null

	if [ $? -eq 0 ]; then 
		let "iexist+=1"
		echo ""
		grep -i "^#### "$newword" " $localdir$dictfile |sed -e "s/XXXXXXXX/\n/g" 
		grep -i "^#### "$newword" " $localdir$dict_ee |sed -e "s/XXXXXXXX/\n/g" 
	fi
	if [ $iexist -eq 0 ]; then 

		badword="$badword $newword"
	fi
	if grep -q -i -w $newword $localdir$dict_except;then
		echored "CAUTION:The word in except dict!"
		echo ""
	fi

done

if [ "$badword" = "" ]; then 

	continue

else

#	echo "请注解一下字词："

	for words in `echo $badword`
	do
		yd $words | sed -e "s/.*\[1G//g" |sed -e "/^$/d" >tmp.md

		if file tmp.md |grep -q empty; then
			echo "没有这个单词:" $words
			exit -1
		fi

		echo "#### "$words" " > /tmp/dict.md 

		cat tmp.md | sed -e "s/^ *//g" |sed -e "/^$/d" |awk '{print $0 "\n"}' >>/tmp/dict.md 

		cat /tmp/dict.md

		echo "" >>$localdir$dictfile
		
		sed -e "s/%/%%/g" /tmp/dict.md | awk '{if (NF == 0) printf "XXXXXXXX";else printf $0"XXXXXXXX"}' |sed -e "s/XXXXXXXX#### /\n\n#### /g" >>$localdir$dictfile

	done
fi

rm -f tmp.md


